package com.cysyz.mylib.web.archetype.ssh.action;


import com.cysyz.json.IJSONGenerator;
import com.cysyz.json.ResultType;
import com.cysyz.json.framework.impl.JSONGeneratorImpl;
import com.cysyz.json.framework.wraper.JSONWraper;
import com.cysyz.mylib.utils.MyReflectionUtils;
import com.cysyz.mylib.utils.myassert.MyAssert;
import com.opensymphony.xwork2.ActionSupport;
import net.sf.json.JsonConfig;
import org.apache.struts2.ServletActionContext;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

public abstract class BaseAction extends ActionSupport{

    protected String responseJSON(List<Object> returnContent, JsonConfig jsonConfig, ResultType resultType, JSONWraper jsonWraper) {
		IJSONGenerator jsonGenerator = new JSONGeneratorImpl(jsonWraper);
		String returnJSON = jsonGenerator.generateJSON(resultType, returnContent, jsonConfig);
		getResponse().setCharacterEncoding("utf-8");
		try {
			getResponse().getWriter().print(returnJSON);
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
		return NONE;
	}
	
	protected String responseJSON(List<Object> returnContent, ResultType resultType) {
		this.responseJSON(returnContent, null, resultType, null);
		return NONE;
	}
	
	protected String responseJSON(List<Object> returnContent, JsonConfig jsonConfig) {
		this.responseJSON(returnContent, jsonConfig, ResultType.SUCCESS, null);
		return NONE;
	}
	protected String responseJSON(List<Object> returnContent) {
		this.responseJSON(returnContent, null, ResultType.SUCCESS, null);
		return NONE;
	}

    protected  String responseJSON(Object... returnContent) {
        List<Object> contents = new ArrayList<Object>();
        for (Object content :returnContent) {
            contents.add(content);
        }
        return responseJSON(contents);
    }

    protected  String responseJSON(JSONWraper jsonWraper, Object... returnContent) {
        List<Object> contents = new ArrayList<Object>();
        for (Object content :returnContent) {
            contents.add(content);
        }
        return responseJSON(contents, null, ResultType.SUCCESS, jsonWraper);
    }

    protected  String responseJSON(JsonConfig jsonConfig, Object... returnContent) {
        List<Object> contents = new ArrayList<Object>();
        for (Object content :returnContent) {
            contents.add(content);
        }
        return responseJSON(contents, jsonConfig);
    }

    protected  String responseJSON(JsonConfig jsonConfig, JSONWraper jsonWraper, Object... returnContent) {
        List<Object> contents = new ArrayList<Object>();
        for (Object content :returnContent) {
            contents.add(content);
        }
        return responseJSON(contents, jsonConfig, ResultType.SUCCESS, jsonWraper);
    }

    protected void writeToResponse(String content) {
		try {
			getResponse().getWriter().print(content);
			getResponse().getWriter().flush();
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
	}

    private Object getSessionAttribute(String key) {
        return this.getSession().getAttribute(key);
    }

    protected  String getRealPath(String path) {
        return this.getRequest().getServletContext().getRealPath(path);
    }

    protected PrintWriter getWriter() throws IOException {
        return this.getResponse().getWriter();
    }

	protected HttpServletResponse getResponse() {
		return ServletActionContext.getResponse();
	}
	
	protected void setRequestAttribute(String key, Object value) {
		this.getRequest().setAttribute(key, value);
	}

	protected HttpServletRequest getRequest() {
		return ServletActionContext.getRequest();
	}

    protected HttpSession getSession() {
        return this.getRequest().getSession();
    }
	
	protected String getMethod() {
		return this.getRequest().getMethod();
	}
	
	protected boolean isPost() {
		return getMethod().equalsIgnoreCase("post");
	}
	
	protected boolean isGet() {
		return getMethod().equalsIgnoreCase("get");
	}
	
	protected String transcodingIfGet(String val) {
		MyAssert.notNull(val);
		String retVal = val;
		if (isGet()) {
			try {
				retVal = new String(retVal.getBytes("iso-8859-1"), "utf-8");
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
		}
		return retVal;
	}
	
    public void transcodingIfGet(Object obj, String... fieldNames) {
        for (String fieldName : fieldNames) {
            Object fieldValue = MyReflectionUtils.getFieldValue(fieldName, obj);
            if (fieldValue instanceof  String) {
                MyReflectionUtils.setFieldValue(obj, fieldName, transcodingIfGet(fieldValue.toString()));
            }
        }
    }
}
